package com.crm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.crm.entity.ClientUser;
import com.crm.entity.Clientdet;
import com.crm.entity.Supplier;
import com.crm.entity.SupplierUser;
import com.crm.util.ApplicationContext;

public class SupplierDAO {
	protected final Log logger = LogFactory.getLog(getClass());

	public  int insert(Supplier c) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     session.saveOrUpdate(c);
		     tx.commit();
		     return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<Supplier> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     List<Supplier> custObjs=new ArrayList<Supplier>();
		     List objs=null;
		     Query query=session.createQuery("from Supplier where  is_deleted!=1 and business_id=:busid");
		     query.setInteger("busid", busid);
		     objs=query.list();
		     for(Object o:objs){
		    	 custObjs.add((Supplier)o);
		     }
		     return custObjs;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  Supplier findById(int i,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     String qrystr="From Supplier sp where sp.id=:sid and business_id=:busid";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("sid", i);
		     qry.setInteger("busid", busid);
		     Object ob=qry.uniqueResult();
		     Supplier s=(Supplier)ob;
		     return s;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  Supplier getByName(String name,int busid) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     String querystr="From Supplier where name=:name and business_id=:busid";
		     Query query=session.createQuery(querystr);
		     query.setString("name", name);
		     query.setInteger("busid", busid);
		     Object queryresult=query.uniqueResult();
		     Supplier comp=(Supplier)queryresult;
		     return comp;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}

	public  List<Supplier> getSpl(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     String qrystr="From Supplier where business_id=:busid";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("busid", busid);
		     List<Supplier> spl=qry.list();
		     return spl;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}

	public  Supplier getSupplier(String suppname,int busid)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="From Supplier where name=:name and business_id=:busid";
			Query query=session.createQuery(querystr);
			query.setString("name", suppname);
			query.setInteger("busid", busid);
			Object queryresult=query.uniqueResult();
			Supplier c=(Supplier)queryresult;
			return c;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  List<SupplierUser> getUser(int id,int busid)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qtr="From SupplierUser where supplierid=:suppid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("suppid", id);
			List<SupplierUser> qresult=qry.list();
			return qresult;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  String dropusers(int compid,int busid)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction(); 
			String qtr="Delete From SupplierUser where companyid=:id and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("id", compid);	
			qry.setInteger("busid", busid);
			qry.executeUpdate();
			tx.commit();
			return "SUCCESS";
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  void deleteSupplier(int suppid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			String qtr="update Supplier set is_deleted=true where id=:id and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("id", suppid);	
			qry.setInteger("busid", busid);
			qry.executeUpdate();
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public Supplier getLastRow(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qtr="From Supplier where business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);
			List<Object> o=qry.list();
			Supplier c=new Supplier();
			for(int i=0;i<o.size();i++){
				c=(Supplier) o.get(i);
			}
			return c;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public int insertUser(SupplierUser sliUser) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(sliUser);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	
}



